class Room
  attr_reader :db_id, :name
  attr_reader :area_id, :terrain, :coord_x, :coord_y, :coord_id, :coord_cont
  attr_reader :healer, :pk, :shop, :safe, :bank, :maze, :questor, :trainer, :guild, :angry, :no_recall, :no_portal
  
  attr_reader :area
  
  @@list    ||= nil
  @@current ||= nil
  @@exits   ||= nil
  
  ##### ACCESSORS #####
  def self.[](index); @@list[index] end
  
  def self.all; @@list end
  def self.current; @@current end
  def self.exits; @@exits end
  def self.exit?(from_id, cmd); @@list[from_id].exits.key?(cmd) end
  def self.set_current(index, exits)
    @@current = @@list[index]
    @@exits = exits
  end
  
  ##### CHECKS #####
  def self.exist?(num) @@list.key?(num) end
  def self.count; @@list.length end
  
  ##### EDITS #####
  def self.add_door(from_id, cmd)
    (echo "No exit that direction.";return) unless Room.exit?(from_id, cmd)
    Room[from_id].exits[cmd]['door'] = !Room[from_id].exits[cmd]['door']
    $db.execute("UPDATE Exit SET door=? WHERE from_id=? AND cmd=?", Room[from_id].exits[cmd]['door'] ? 1 : 0, from_id, cmd)
  end
  
  ##### INITIALIZATION #####
  def self.init(db_id)
    @@list = {} if @@list.nil?
    room_row = $db.get_first_row("
      SELECT id, name, area_id, terrain, coord_x, coord_y, coord_id, coord_cont, healer, pk
           , shop, safe, bank, maze, questor, trainer, guild, angry, no_recall, no_portal
      FROM Room WHERE id = ?", db_id
    )
    Room.new(room_row)
  end
  
  def initialize(room_row)
    @db_id, @name, @area_id, @terrain, @coord_x, @coord_y, @coord_id, @coord_cont, @healer, @pk,
      @shop, @safe, @bank, @maze, @questor, @trainer, @guild, @angry, @no_recall, @no_portal = room_row
    
    @area = Area[@area_id]
    @@list[@db_id] = self
    self.load_exits
  end
    
  def load_exits
    @wayto, @exits = {}, {}
    $db.execute("
      SELECT id, from_id, to_id, cmd, door, cost, key
      FROM Exit WHERE from_id = ?", @db_id
    ) { |r|
      @wayto[r[2]] = {'id'=>r[0], 'from_id'=>r[1], 'to_id'=>r[2], 'cmd'=>r[3], 'door'=>r[4]==1, 'cost'=>r[5], 'key'=>r[6]}
      @exits[r[3]] = @wayto[r[2]]
    }
  end
  
  ##### DB STUFF #####
  def self.create(id, name, zone, terrain, details, exits, coord)
    DBRoom.create($db, id, name, zone, terrain, details, coord)
    exits.each {|cmd, to_room| DBExit.create($db, id, to_room, cmd) unless to_room == -1}
    self.init(id)
  end
  
  def self.add_exit(from_id, to_id, cmd, door = false, cost = 1)
    DBExit.create($db, from_id, to_id, cmd, door, cost)
    Room[from_id].load_exits
  end
  def self.del_exit(from_id, to_id = nil, cmd = nil)
    return if to_id.nil? && cmd.nil?
    
    args = {'from_id'=>from_id}
    args['to_id'] = to_id unless to_id.nil?
    args['cmd'] = cmd unless cmd.nil?
    $db.execute("
      DELETE FROM Exit
      WHERE from_id = :from_id
        #{to_id.nil? ? "" : "AND to_id = :to_id"}
        #{cmd.nil? ? "" : "AND cmd = :cmd"}", args
    )
    Room.current.load_exits
  end
  
  def wayto; @wayto.dup end
  def exits; @exits.dup end
end